package com.no1ikun.view;

import com.no1ikun.bean.Student;
import com.no1ikun.service.StudentService;
import com.no1ikun.service.StudentServiceImpl;
import com.no1ikun.util.CalendarPanel;
import com.no1ikun.util.MyButton;
import com.no1ikun.util.MyJLabel;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateJPanel extends JPanel {
    private MyJTextField nameEdit;
    private MyJTextField passwordEdit;
    private MyJTextField sexEdit;

    private MyJTextField birthdayEdit;
    private MyJTextField sno_edit;
    private MyJTextField native_edit;
    private MyJTextField earth_edit;
    private MyJTextField cno_edit;
    private StudentService ss = new StudentServiceImpl();

    public UpdateJPanel() {
        //初始化数据
        init();
    }


    public void init() {
        this.setLayout(null);//绝对布局
        this.setVisible(false);

        this.setBounds(80, 80, 885, 452);
        this.setBackground(Color.WHITE);
        //添加文字
        MyJLabel maxText = new MyJLabel("学号", 100, 40, 100, 20, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel nameText = new MyJLabel("姓名", 40, 25, 110, 80, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel passwordText = new MyJLabel("密码", 40, 25, 110, 120, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel sexText = new MyJLabel("性别", 40, 25, 110, 160, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel birthdayText = new MyJLabel("生日", 40, 25, 110, 200, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel native_place = new MyJLabel("籍贯", 40, 25, 110, 240, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel earth_place = new MyJLabel("生源地", 60, 25, 90, 280, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel cno = new MyJLabel("班级", 40, 25, 110, 320, "微软雅黑", 1, 17, new Color(28, 136, 201));



        //添加编辑框
        sno_edit = new MyJTextField(200, 30, 155, 25);
        sno_edit.setBorder(new LineBorder(new Color(28, 136, 201)));
        nameEdit = new MyJTextField(160, 80, 235, 25);
        nameEdit.setBorder(new LineBorder(new Color(28, 136, 201)));
        passwordEdit = new MyJTextField(160, 120, 235, 25);
        passwordEdit.setBorder(new LineBorder(new Color(28, 136, 201)));
        sexEdit = new MyJTextField(160, 160, 235, 25);
        sexEdit.setBorder(new LineBorder(new Color(28, 136, 201)));

        birthdayEdit = new MyJTextField(160, 200, 235, 25);
        birthdayEdit.setBorder(new LineBorder(new Color(28, 136, 201)));
        CalendarPanel calendarPanel = new CalendarPanel(birthdayEdit, "yyyy-MM-dd");
        calendarPanel.initCalendarPanel();
        calendarPanel.setBounds(150,40,251, 245);
        JLabel tips = new JLabel("日历面板");
        calendarPanel.add(tips);
        native_edit = new MyJTextField(160, 240, 235, 25);
        native_edit.setBorder(new LineBorder(new Color(28, 136, 201)));
        earth_edit = new MyJTextField(160, 280, 235, 25);
        earth_edit.setBorder(new LineBorder(new Color(28, 136, 201)));
        cno_edit = new MyJTextField(160, 320, 235, 25);
        cno_edit.setEnabled(false);
        cno_edit.setBorder(new LineBorder(new Color(28, 136, 201)));

        //添加两个按钮
        MyButton update = new MyButton("修改", 180, 400, 70, 25, new Color(28, 136, 201), Color.WHITE);
        MyButton select = new MyButton("查询", 380, 30, 70, 25, new Color(28, 136, 201), Color.WHITE);
        MyButton reset = new MyButton("重置", 300, 400, 70, 25, new Color(28, 136, 201), Color.WHITE);
        this.add(calendarPanel);
        this.add(select);
        this.add(sno_edit);
        this.add(maxText);
        this.add(native_edit);
        this.add(earth_edit);
        this.add(cno_edit);
        this.add(native_place);
        this.add(earth_place);
        this.add(cno);

        this.add(update);
        this.add(reset);
        this.add(nameEdit);
        this.add(passwordEdit);
        this.add(sexEdit);
        this.add(birthdayEdit);

        this.add(maxText);
        this.add(nameText);
        this.add(passwordText);
        this.add(sexText);

        this.add(birthdayText);

        //查詢按鈕的监听事件
        select.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //要去查收是否有这个学生,如果有这个学生则需要返回学生对象
                String info = sno_edit.getText();
                //去查询
                Student stu = ss.selectByNumberOrPhone(info);
                if (stu != null) {
                    //将学生的数据显示在指定的空间中
                    nameEdit.setText(stu.getSname());
                    passwordEdit.setText(stu.getSpwd());
                    sexEdit.setText(stu.getSex());
                    birthdayEdit.setText(stu.getBirthday());
                    native_edit.setText(stu.getNative_place() + "");
                    earth_edit.setText(stu.getEarth_place() + "");
                    cno_edit.setText(stu.getCno() + "");

                } else {
                    JOptionPane.showMessageDialog(null, "查无此人", "温馨提示", JOptionPane.WARNING_MESSAGE);
                    clearText();
                }

            }
        });
        //监听修改按钮事件
        update.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取所有的数据
                String snoText = sno_edit.getText();
                String nameText = nameEdit.getText();
                String passwordEditText = passwordEdit.getText();
                String sexEditText = sexEdit.getText();
                String birthdayEditText = birthdayEdit.getText();
                String native_editText = native_edit.getText();
                String earth_editText = earth_edit.getText();
                String cno_editText = cno_edit.getText();
//                System.out.println(snoText+"学号");
//                System.out.println(nameText+"姓名");
//                System.out.println(passwordEditText+"密码");
                Student stu = new Student(snoText,nameText,passwordEditText,sexEditText,birthdayEditText,native_editText,earth_editText,cno_editText);

                //去完成修改操作
                int i = ss.updateByNumberOrPhone(stu);
                if (i > 0) {
                    JOptionPane.showMessageDialog(null, "恭喜修改成功!", "温馨提示", JOptionPane.WARNING_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "修改失败", "温馨提示", JOptionPane.WARNING_MESSAGE);
                }

            }
        });
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clearText();
            }
        });
    }

    public void clearText() {
        nameEdit.setText("");
        passwordEdit.setText("");
        sexEdit.setText("");
        birthdayEdit.setText("");
        native_edit.setText("");
        earth_edit.setText("");
        cno_edit.setText("");
    }

}
